****APPENDIX TABLE C14****

cd "$main_dir/output/tables/appendix_c"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc)

local varlist prop_unrest no_exec_changes prop_affect_epidemic bank_crisis currency_crisis twin_crisis
foreach y of local varlist {
	merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/`y'_var_exp.dta", nogen keepusing(total_`y'_obs w`y'_1)
}

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/winsz_inflation_var_exp.dta", nogen keepusing(total_winsz_inflation_obs wwinsz_inflation_1 sd_winsz_inflation)

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Run regressions
//--------------------------------------------------------

matrix coef1 = J(8, 3, .)
	matrix coln coef1 = coefficients lcl ucl
	matrix rown coef1 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"

matrix coef2 = J(8, 3, .)
	matrix coln coef2 = coefficients lcl ucl
	matrix rown coef2 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"
	
matrix coef3 = J(8, 3, .)
	matrix coln coef3 = coefficients lcl ucl
	matrix rown coef3 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"

matrix coef4 = J(8, 3, .)
	matrix coln coef4 = coefficients lcl ucl
	matrix rown coef4 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"

local i = 1
local varlist prop_unrest no_exec_changes prop_affect_epidemic bank_crisis currency_crisis twin_crisis winsz_inflation
foreach var of local varlist {
	
	qui reghdfe trust_govt wgrowth_rate_1 w`var'_1 yob_ln_gdppc if sample==1 & total_`var'_obs>=0.9*(age-1) & total_`var'_obs!=., absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode)
	
	cap drop z*
	local list trust_govt wgrowth_rate_1 yob_ln_gdppc w`var'_1
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_govt zwgrowth_rate_1 zw`var'_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(90)
	
	qui mat os = r(table)
	qui mat coef1[`i',1] = os[1,1]
	qui mat coef1[`i',2] = os[5,1]
	qui mat coef1[`i',3] = os[6,1]
	
	qui mat coef2[`i',1] = os[1,2]
	qui mat coef2[`i',2] = os[5,2]
	qui mat coef2[`i',3] = os[6,2]
	
	qui mat coef3[`i',1] = os[1,1]
	qui mat coef3[`i',2] = os[1,1]-1.96*os[2,1]
	qui mat coef3[`i',3] = os[1,1]+1.96*os[2,1]
	
	qui mat coef4[`i',1] = os[1,2]
	qui mat coef4[`i',2] = os[1,2]-1.96*os[2,2]
	qui mat coef4[`i',3] = os[1,2]+1.96*os[2,2]
	
	qui test _b[zwgrowth_rate_1] = _b[zw`var'_1]
	qui scalar p_`var' = r(p)
	qui scalar F_`var' = r(F)
	
	di "`var' done"
	
	local ++i
}

qui reghdfe trust_govt wgrowth_rate_1 sd_winsz_inflation yob_ln_gdppc if sample==1 & total_winsz_inflation_obs>=0.9*(age-1) & total_winsz_inflation_obs!=., absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(95)
	
cap drop z*
local list trust_govt wgrowth_rate_1 yob_ln_gdppc sd_winsz_inflation
foreach x of local list {
	egen z`x' = std(`x') if e(sample)==1
}

qui reghdfe ztrust_govt zwgrowth_rate_1 zsd_winsz_inflation zyob_ln_gdppc if e(sample)==1, absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(90)

qui test _b[zwgrowth_rate_1] = _b[zsd_winsz_inflation]
qui scalar p_sd_winsz_inflation = r(p)
qui scalar F_sd_winsz_inflation = r(F)

//--------------------------------------------------------
//Create Table C14
//--------------------------------------------------------

mat results = J(2, 8, .)
local varlist prop_unrest no_exec_changes prop_affect_epidemic bank_crisis currency_crisis twin_crisis winsz_inflation sd_winsz_inflation
local i = 1
foreach var of local varlist {
    mat results[1, `i'] = F_`var'
    mat results[2, `i'] = p_`var'
    local ++i
}

mat colnames results = "Unrest" "Executive_Turnover" "Epidemic_Exposure" "Baking_Crisis" "Currency_Crisis" "Financial_Crisis" "Inflation" "Inflation_Volatility"
mat rownames results = "F-test" "p-value"

svmat double results, names(col)
drop if Unrest==.
keep Unrest Executive_Turnover Epidemic_Exposure Baking_Crisis Currency_Crisis Financial_Crisis Inflation Inflation_Volatility
export delimited using "table_c14.csv", replace
